package com.ysjy.course.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.PromptMessage;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.StudentInfoDto;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.handler.JsonUtil;
import com.wkbb.common.utils.StringUtil;
import com.wkbb.sports.mapper.BasicMapper;
import com.ysjy.course.model.OrgCourse;
import com.ysjy.course.model.YsTrusteeship;
import com.ysjy.course.service.TrusteeshipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(description = "托管服务管理")
@RequestMapping("/trusteeship/user")
@Slf4j
public class TrusteeshipController extends BaseController {

    @Autowired
    private TrusteeshipService trusteeshipService;
    @Autowired
    private BasicMapper basicMapper;

    @ApiOperation(value = "获取托管服务列表")
    @RequestMapping(value = "getTrusteeshipListByPage", method = RequestMethod.GET)
    public ResultDTO<PageInfo<YsTrusteeship>> getTrusteeshipListByPage(
            @ApiParam(value = "学生ID", required = false) @RequestParam(name = "studentId", required = false) Long studentId,
            @ApiParam(value = "分类ID", required = false) @RequestParam(name = "tagId", required = false) Long tagId,
            @ApiParam(value = "页码", required = true, defaultValue = "1") @RequestParam(name = "pageNo", required = true, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数", required = true, defaultValue = "10") @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        UserDto userDto = getRouteUserByUnion();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        if (StringUtils.isEmpty(studentId)){
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_STU_NOT_EXIST);
        }
        StudentInfoDto studentInfoDto = basicMapper.getStudentInfoByStudentId(studentId);
        log.info("studentInfoDto：{}", JsonUtil.objectToJson(studentInfoDto));
        if (studentInfoDto != null){
            try {
                Map<String, Object> params = new HashMap<>();
                params.put("tagId", tagId);
                params.put("orgId", studentInfoDto.getOrgId());
                params.put("gradeId",studentInfoDto.getGradeId());
                //报名状态 0未开始、1报名中、2已结束、3已报名、4已报满
                params.put("signUpStatus", 1);
                params.put("shelvesStatus", 1);
                PageInfo<YsTrusteeship> pageInfo = trusteeshipService.getListByPage(params, pageNo, pageSize);
                return ResultDTO.successResult(pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
    }

    @ApiOperation(value = "获取托管服务详情")
    @RequestMapping(value = "getTrusteeship", method = RequestMethod.GET)
    public ResultDTO<YsTrusteeship> getTrusteeship(
            @ApiParam(value = "学生ID", required = false) @RequestParam(name = "studentId", required = false) Long studentId,
            @ApiParam(value = "唯一标识", required = true) @RequestParam(name = "uniqueId", required = true) String uniqueId) {
        try {
            UserDto userDto = getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            if (StringUtils.isEmpty(studentId)){
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_STU_NOT_EXIST);
            }
            YsTrusteeship data = trusteeshipService.getTrusteeshipByUniqueId(uniqueId,studentId);
            if (data != null) return ResultDTO.successResult(data);
            else return ResultDTO.errorResult(PromptMessage.LOAD_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

}